from telnetlib import DO

import numpy as np
from manim import *
from numpy import flip, insert
import math

LV, QL, QH, JH = '#75cfb8', '#bbdfc8', '#f0e5d8', '#ffc478'

class p1(MovingCameraScene):
    def construct(self):
        # self.camera.frame.save_state()
        # self.add(NumberPlane())
        # self.camera.frame.rotate(13*DEGREES,about_point=[0.0, 0.0, 1.0])
        ab=Line(LEFT*2.5,RIGHT*2.5).shift(UP*2+RIGHT*0.5)
        cd=Line(LEFT*2.5,RIGHT*2.5).shift(DOWN*2+LEFT*0.5)
        a,b,c,d=ab.get_left(),ab.get_right(),cd.get_right(),cd.get_left()
        e,f,g,h=ab.get_center(),(b+c)/2,cd.get_center(),(a+d)/2
        af,ag,bg,bh,ch,ce,de,df=Line(a,f),Line(a,g),Line(b,g),Line(b,h),Line(c,h),Line(c,e),Line(d,e),Line(d,f)
        self.add(ab,cd,af,ag,bg,bh,ch,ce,de,df,
                 Line(b,c),
                 Line(a,d),
                 MathTex('A').next_to(a,UL),
                 MathTex('B').next_to(b,UR),
                 MathTex('C').next_to(c,DR),
                 MathTex('D').next_to(d,DL),
                 MathTex('E').next_to(e,UP),
                 MathTex('F').next_to(f),
                 MathTex('G').next_to(g,DOWN),
                 MathTex('H').next_to(h,LEFT)
                 )
        # 八边形
        o=ORIGIN
        o1,o3,o5,o7=(e+o)/2,(o+f)/2,(o+g)/2,(o+h)/2
        o2,o4,o6,o8=(o+b)/3,(o+c)/3,(o+d)/3,(o+a)/3
        self.add(Polygon(o1,o2,o3,o4,o5,o6,o7,o8).set_fill(LV,1))

class p2(MovingCameraScene):
    def construct(self):
        # self.camera.frame.save_state()
        # self.add(NumberPlane())
        # self.camera.frame.rotate(13*DEGREES,about_point=[0.0, 0.0, 1.0])
        ab=Line(LEFT*2.5,RIGHT*2.5).shift(UP*2+RIGHT*0.5)
        cd=Line(LEFT*2.5,RIGHT*2.5).shift(DOWN*2+LEFT*0.5)
        a,b,c,d=ab.get_left(),ab.get_right(),cd.get_right(),cd.get_left()
        e,f,g,h=ab.get_center(),(b+c)/2,cd.get_center(),(a+d)/2
        af,ag,bg,bh,ch,ce,de,df=Line(a,f),Line(a,g),Line(b,g),Line(b,h),Line(c,h),Line(c,e),Line(d,e),Line(d,f)
        self.add(ab,cd,af,ag,bg,bh,ch,ce,de,df,
                 Line(b,c),
                 Line(a,d),
                 MathTex('A').next_to(a,UL),
                 MathTex('B').next_to(b,UR),
                 MathTex('C').next_to(c,DR),
                 MathTex('D').next_to(d,DL),
                 MathTex('E').next_to(e,UP),
                 MathTex('F').next_to(f),
                 MathTex('G').next_to(g,DOWN),
                 MathTex('H').next_to(h,LEFT)
                 )
        # 八边形
        o=ORIGIN
        o1,o3,o5,o7=(e+o)/2,(o+f)/2,(o+g)/2,(o+h)/2
        o2,o4,o6,o8=(o+b)/3,(o+c)/3,(o+d)/3,(o+a)/3
        self.add(Polygon(o1,o2,o3,o4,o5,o6,o7,o8).set_fill(LV,1))
        # p2
        eg,fh=Line(e,g),Line(f,h)
        self.add(eg,fh)

class p3(MovingCameraScene):
    def construct(self):
        # self.camera.frame.save_state()
        # self.add(NumberPlane())

        # self.camera.frame.rotate(13*DEGREES,about_point=[0.0, 0.0, 1.0])
        ab=Line(LEFT*2.5,RIGHT*2.5).shift(UP*2+RIGHT*0.5)
        cd=Line(LEFT*2.5,RIGHT*2.5).shift(DOWN*2+LEFT*0.5)
        a,b,c,d=ab.get_left(),ab.get_right(),cd.get_right(),cd.get_left()
        e,f,g,h=ab.get_center(),(b+c)/2,cd.get_center(),(a+d)/2
        af,ag,bg,bh,ch,ce,de,df=Line(a,f),Line(a,g),Line(b,g),Line(b,h),Line(c,h),Line(c,e),Line(d,e),Line(d,f)
        self.add(ab,cd,af,ag,bg,bh,ch,ce,de,df,
                 Line(b,c),
                 Line(a,d),
                 MathTex('A').next_to(a,UL),
                 MathTex('B').next_to(b,UR),
                 MathTex('C').next_to(c,DR),
                 MathTex('D').next_to(d,DL),
                 MathTex('E').next_to(e,UP),
                 MathTex('F').next_to(f),
                 MathTex('G').next_to(g,DOWN),
                 MathTex('H').next_to(h,LEFT)
                 )
        # 八边形
        o=ORIGIN
        o1,o3,o5,o7=(e+o)/2,(o+f)/2,(o+g)/2,(o+h)/2
        o2,o4,o6,o8=(o+b)/3,(o+c)/3,(o+d)/3,(o+a)/3
        self.add(Polygon(o1,o2,o3,o4,o5,o6,o7,o8).set_fill(LV,1))
        # p2
        eg,fh=Line(e,g),Line(f,h)
        self.add(eg,fh)
        # p3
        self.camera.frame.scale(0.5).move_to((e+f)/2)
        mid_ef=(e+f)/2
        self.add(
            MathTex('O').next_to(o,DR),
            Line(o1,(b+f)/2).set_color(JH),
            Line(o3,(b+e)/2).set_color(JH),
            Line((o+o1)/2,(o3+mid_ef)/2).set_color(RED),
            Line((o+o3)/2,(o1+mid_ef)/2).set_color(RED)
        )

class p4(MovingCameraScene):
    def construct(self):
        # self.camera.frame.save_state()
        # self.add(NumberPlane())

        # self.camera.frame.rotate(13*DEGREES,about_point=[0.0, 0.0, 1.0])
        ab=Line(LEFT*2.5,RIGHT*2.5).shift(UP*2+RIGHT*0.5)
        cd=Line(LEFT*2.5,RIGHT*2.5).shift(DOWN*2+LEFT*0.5)
        a,b,c,d=ab.get_left(),ab.get_right(),cd.get_right(),cd.get_left()
        e,f,g,h=ab.get_center(),(b+c)/2,cd.get_center(),(a+d)/2
        af,ag,bg,bh,ch,ce,de,df=Line(a,f),Line(a,g),Line(b,g),Line(b,h),Line(c,h),Line(c,e),Line(d,e),Line(d,f)
        self.add(ab,cd,af,ag,bg,bh,ch,ce,de,df,
                 Line(b,c),
                 Line(a,d),
                 MathTex('A').next_to(a,UL),
                 MathTex('B').next_to(b,UR),
                 MathTex('C').next_to(c,DR),
                 MathTex('D').next_to(d,DL),
                 MathTex('E').next_to(e,UP),
                 MathTex('F').next_to(f),
                 MathTex('G').next_to(g,DOWN),
                 MathTex('H').next_to(h,LEFT)
                 )
        # 八边形
        o=ORIGIN
        o1,o3,o5,o7=(e+o)/2,(o+f)/2,(o+g)/2,(o+h)/2
        o2,o4,o6,o8=(o+b)/3,(o+c)/3,(o+d)/3,(o+a)/3
        self.add(Polygon(o1,o2,o3,o4,o5,o6,o7,o8).set_fill(LV,1))
        # p2
        eg,fh=Line(e,g),Line(f,h)
        self.add(eg,fh)
        # p3
        self.camera.frame.scale(0.5).move_to((e+f)/2)
        mid_ef=(e+f)/2
        self.add(
            MathTex('O').next_to(o,DR),
            Line(o1,(b+f)/2).set_color(JH),
            Line(o3,(b+e)/2).set_color(JH),
            Line((o+o1)/2,(o3+mid_ef)/2).set_color(RED),
            Line((o+o3)/2,(o1+mid_ef)/2).set_color(RED)
        )
        # p4
        self.camera.frame.scale(0.5).move_to((mid_ef+o)/2)
        self.add(
            MathTex(r'\frac{p}{64}').scale(0.5).shift(UR/4+RIGHT/8),
            MathTex(r'\frac{3}{4}\cdot\frac{p}{64}').scale(0.5).shift(UP/4+RIGHT*0.9),
            MathTex(r'\frac{3}{4}\cdot\frac{p}{64}').scale(0.5).shift(UP*0.7 + RIGHT/2)
        )

class p5(MovingCameraScene):
    def construct(self):
        # self.camera.frame.save_state()
        # self.add(NumberPlane())
        # self.camera.frame.rotate(13*DEGREES,about_point=[0.0, 0.0, 1.0])
        ab=Line(LEFT*2.5,RIGHT*2.5).shift(UP*2+RIGHT*0.5)
        cd=Line(LEFT*2.5,RIGHT*2.5).shift(DOWN*2+LEFT*0.5)
        a,b,c,d=ab.get_left(),ab.get_right(),cd.get_right(),cd.get_left()
        e,f,g,h=ab.get_center(),(b+c)/2,cd.get_center(),(a+d)/2
        af,ag,bg,bh,ch,ce,de,df=Line(a,f),Line(a,g),Line(b,g),Line(b,h),Line(c,h),Line(c,e),Line(d,e),Line(d,f)
        self.add(ab,cd,af,bg,ch,de,
                 Line(b,c),
                 Line(a,d),
                 MathTex('A').next_to(a,UL),
                 MathTex('B').next_to(b,UR),
                 MathTex('C').next_to(c,DR),
                 MathTex('D').next_to(d,DL),
                 MathTex('E').next_to(e,UP),
                 MathTex('F').next_to(f),
                 MathTex('G').next_to(g,DOWN),
                 MathTex('H').next_to(h,LEFT)
                 )
        # 八边形
        o=ORIGIN
        self.add(Polygon(af.point_from_proportion(2/5),bg.point_from_proportion(2/5),ch.point_from_proportion(2/5),de.point_from_proportion(2/5)).set_fill(LV,1))